package jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;

public class MetadataDemo {
	public static void main(String[] args) throws Exception {
		DBMetadata();
//		 ResultSetMeta("s_emp");
	}

	private static void ResultSetMeta(String tableName) throws Exception {
		String sql = "select * from " + tableName;
		Connection con = null;
		Statement stm = null;
		ResultSet rs = null;
		try {
			con = DBUtil.open();
			stm = con.prepareStatement(sql);
			rs = stm.executeQuery(sql);
			ResultSetMetaData m = rs.getMetaData();
			int columns = m.getColumnCount();

			while (rs.next()) {
				for (int i = 1; i <= columns; i++) {
					int type = m.getColumnType(i);
					String columnName = m.getColumnName(i);
					switch (type) {
					case Types.VARCHAR:
						System.out.println(columnName+":"+rs.getString(i));
						break;
					case Types.INTEGER:
						System.out.println(columnName+":"+rs.getInt(i));
						break;
					case Types.NUMERIC:
						System.out.println(columnName+":"+rs.getDouble(i));
						break;
					case Types.DATE:
					case Types.TIME:
					case Types.TIMESTAMP:
						System.out.println(columnName+":"+rs.getTimestamp(i));
						break;
					default:
						break;
					}
				}
			}
		} finally {
			DBUtil.close(con, stm, rs);
		}

	}

	private static void DBMetadata() throws Exception {
		Connection con = null;
		try {
			con = DBUtil.open();
			DatabaseMetaData m = con.getMetaData();
			System.out.println(m.getDatabaseMajorVersion());
			System.out.println(m.getDatabaseMinorVersion());
			System.out.println(m.getDatabaseProductName());
			System.out.println(m.getDatabaseProductVersion());
			System.out.println(m.getURL());
			System.out.println(m.getUserName());
			System.out.println(m.getCatalogSeparator());
			System.out.println("*******************");
			System.out.println(m.supportsOuterJoins());
			System.out.println(m.supportsExpressionsInOrderBy());
			System.out.println(m.supportsFullOuterJoins());
			System.out
					.println(m
							.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED));
			System.out.println(m.supportsTransactions());
		} finally {
			DBUtil.close(con, null, null);
		}
	}
}
